namespace QMVnSoft.SystemForms
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using QMVnSoft.Controls.ChildGridControls;
    using QMVnSoft.Properties;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormGroupLister : FormControl
    {
        private ToolStripButton btnAdd;
        private ToolStripButton btnClose;
        private ToolStripButton btnDelete;
        private ToolStripButton btnEdit;
        private ToolStripButton btnRefresh;
        private ToolStripButton btnRight;
        private IContainer components;
        public GridControl grdBrowser;
        private object[][] is_ColNames;
        private string is_CurrentCode = "";
        private string is_FieldName = "GRPCODE";
        private string is_LocalKeyField = "Stt";
        private string is_LocalTableName = "GRPLIST";
        private string is_OrderFieldNames = "GRPCODE";
        private DataRow oCurrentRow;
        private FormGroupEditor oFormEditor;
        private DataSet oLocalDataSet;
        private ToolStripSeparator tbrSep01;
        private ToolStripSeparator tbrSep02;
        private ToolStripSeparator tbrSep04;
        private ToolStripSeparator tbrSep05;
        private ToolStrip Toolbars;
        private TbarSearch txtSearch;

        public FormGroupLister()
        {
            object[][] objArray = new object[3][];
            object[] objArray2 = new object[4];
            objArray2[0] = "GrpCode";
            objArray2[1] = "Nhóm";
            objArray2[2] = "C";
            objArray[0] = objArray2;
            object[] objArray3 = new object[4];
            objArray3[0] = "GrpName";
            objArray3[1] = "Tên nhóm";
            objArray3[2] = "C";
            objArray[1] = objArray3;
            object[] objArray4 = new object[4];
            objArray4[0] = "GrpType";
            objArray4[1] = "Quản trị";
            objArray4[2] = "B";
            objArray[2] = objArray4;
            this.is_ColNames = objArray;
            this.InitializeComponent();
            this.grdBrowser.AutoGenerateColumns = false;
            this.grdBrowser.ReadOnly = true;
            this.grdBrowser.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.grdBrowser.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            this.OnFillData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.OnAdd();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            this.OnDelete();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.OnEdit();
        }

        private void btnPassword_Click(object sender, EventArgs e)
        {
            if (((this.LocalTable != null) && (this.LocalTable.Rows.Count != 0)) && (AppInstance.UserName == this.LocalCurrentRow["UserName"].ToString()))
            {
                FormUserPassword password = new FormUserPassword();
                password.UserOrder = (long) this.LocalCurrentRow["Stt"];
                password.ShowDialog();
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.OnRefresh();
            if (!this.OrderFieldNames.Equals(string.Empty))
            {
                ((BindingSource) this.LocalGridBrowser.DataSource).Sort = this.OrderFieldNames;
            }
        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            if (((bool) this.LocalCurrentRow["NotDelete"]) || ((AppInstance.UserType == 1) && ((bool) this.LocalCurrentRow["GrpType"])))
            {
                MessageBox.Show("Không được phân quyền cho người sử dụng này!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                new FormUserRights("G", this.LocalCurrentRow[this.LocalKeyField]).Show();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void grdBrowser_DoubleClick(object sender, EventArgs e)
        {
            this.OnEdit();
        }

        private void InitializeComponent()
        {
            this.Toolbars = new System.Windows.Forms.ToolStrip();
            this.btnRefresh = new System.Windows.Forms.ToolStripButton();
            this.tbrSep05 = new System.Windows.Forms.ToolStripSeparator();
            this.btnAdd = new System.Windows.Forms.ToolStripButton();
            this.btnEdit = new System.Windows.Forms.ToolStripButton();
            this.btnDelete = new System.Windows.Forms.ToolStripButton();
            this.tbrSep01 = new System.Windows.Forms.ToolStripSeparator();
            this.txtSearch = new QMVnSoft.Controls.TbarSearch();
            this.tbrSep02 = new System.Windows.Forms.ToolStripSeparator();
            this.btnRight = new System.Windows.Forms.ToolStripButton();
            this.tbrSep04 = new System.Windows.Forms.ToolStripSeparator();
            this.btnClose = new System.Windows.Forms.ToolStripButton();
            this.grdBrowser = new QMVnSoft.Controls.GridControl();
            this.Toolbars.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.grdBrowser)).BeginInit();
            this.SuspendLayout();
            // 
            // Toolbars
            // 
            this.Toolbars.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnRefresh,
            this.tbrSep05,
            this.btnAdd,
            this.btnEdit,
            this.btnDelete,
            this.tbrSep01,
            this.txtSearch,
            this.tbrSep02,
            this.btnRight,
            this.tbrSep04,
            this.btnClose});
            this.Toolbars.Location = new System.Drawing.Point(0, 0);
            this.Toolbars.Name = "Toolbars";
            this.Toolbars.Size = new System.Drawing.Size(847, 25);
            this.Toolbars.TabIndex = 0;
            this.Toolbars.Text = "Toolbars";
            // 
            // btnRefresh
            // 
            this.btnRefresh.Image = global::QMVnSoft.Properties.Resources.REFRESH;
            this.btnRefresh.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnRefresh.Name = "btnRefresh";
            this.btnRefresh.Size = new System.Drawing.Size(66, 22);
            this.btnRefresh.Text = "Refresh";
            this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
            // 
            // tbrSep05
            // 
            this.tbrSep05.Name = "tbrSep05";
            this.tbrSep05.Size = new System.Drawing.Size(6, 25);
            // 
            // btnAdd
            // 
            this.btnAdd.Image = global::QMVnSoft.Properties.Resources.BALANCE;
            this.btnAdd.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new System.Drawing.Size(58, 22);
            this.btnAdd.Text = "Thêm";
            this.btnAdd.ToolTipText = "Thêm mới (F2)";
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            // 
            // btnEdit
            // 
            this.btnEdit.Image = global::QMVnSoft.Properties.Resources.EDIT;
            this.btnEdit.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnEdit.Name = "btnEdit";
            this.btnEdit.Size = new System.Drawing.Size(46, 22);
            this.btnEdit.Text = "Sửa";
            this.btnEdit.ToolTipText = "Sửa (F3)";
            this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
            // 
            // btnDelete
            // 
            this.btnDelete.Image = global::QMVnSoft.Properties.Resources.DELETE;
            this.btnDelete.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new System.Drawing.Size(45, 22);
            this.btnDelete.Text = "xóa";
            this.btnDelete.ToolTipText = "Xóa (F8)";
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            // 
            // tbrSep01
            // 
            this.tbrSep01.Name = "tbrSep01";
            this.tbrSep01.Size = new System.Drawing.Size(6, 25);
            // 
            // txtSearch
            // 
            this.txtSearch.Name = "txtSearch";
            this.txtSearch.Size = new System.Drawing.Size(100, 25);
            this.txtSearch.Text = "Tìm nhanh...";
            this.txtSearch.ToolTipText = "F4";
            // 
            // tbrSep02
            // 
            this.tbrSep02.Name = "tbrSep02";
            this.tbrSep02.Size = new System.Drawing.Size(6, 25);
            // 
            // btnRight
            // 
            this.btnRight.Image = global::QMVnSoft.Properties.Resources.USER_RIGHT;
            this.btnRight.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnRight.Name = "btnRight";
            this.btnRight.Size = new System.Drawing.Size(90, 22);
            this.btnRight.Text = "Phân quyền";
            this.btnRight.ToolTipText = "Phân quyền sử dụng";
            this.btnRight.Click += new System.EventHandler(this.btnRight_Click);
            // 
            // tbrSep04
            // 
            this.tbrSep04.Name = "tbrSep04";
            this.tbrSep04.Size = new System.Drawing.Size(6, 25);
            // 
            // btnClose
            // 
            this.btnClose.Image = global::QMVnSoft.Properties.Resources.CLOSE;
            this.btnClose.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnClose.Name = "btnClose";
            this.btnClose.Size = new System.Drawing.Size(58, 22);
            this.btnClose.Text = "Thoát";
            this.btnClose.ToolTipText = "Thoát (Esc)";
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // grdBrowser
            // 
            this.grdBrowser.AllowUserToAddRows = false;
            this.grdBrowser.AllowUserToDeleteRows = false;
            this.grdBrowser.CheckColor = false;
            this.grdBrowser.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.grdBrowser.Dock = System.Windows.Forms.DockStyle.Fill;
            this.grdBrowser.Font = new System.Drawing.Font("Arial", 8.25F);
            this.grdBrowser.Location = new System.Drawing.Point(0, 25);
            this.grdBrowser.Name = "grdBrowser";
            this.grdBrowser.Size = new System.Drawing.Size(847, 241);
            this.grdBrowser.TabIndex = 1;
            this.grdBrowser.DoubleClick += new System.EventHandler(this.grdBrowser_DoubleClick);
            // 
            // FormGroupLister
            // 
            this.ClientSize = new System.Drawing.Size(847, 266);
            this.Controls.Add(this.grdBrowser);
            this.Controls.Add(this.Toolbars);
            this.Name = "FormGroupLister";
            this.Text = "Danh sách nhóm người sử dụng";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Toolbars.ResumeLayout(false);
            this.Toolbars.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.grdBrowser)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public virtual bool OnAdd()
        {
            this.LocalFormEditor = new FormGroupEditor();
            if (this.LocalFormEditor != null)
            {
                this.LocalFormEditor.LocalFormLister = this;
                this.LocalFormEditor.NewChecked = true;
                this.LocalFormEditor.LocalCurrentRow = this.LocalCurrentRow;
                this.LocalFormEditor.Show();
            }
            return true;
        }

        public virtual bool OnDelete()
        {
            if ((this.LocalGridBrowser.SelectedRows == null) || (this.LocalGridBrowser.SelectedRows.Count == 0))
            {
                return false;
            }
            if (((bool) this.LocalCurrentRow["NotDelete"]) || ((AppInstance.UserType == 1) && ((bool) this.LocalCurrentRow["GrpType"])))
            {
                MessageBox.Show("Không xóa được nhóm này!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (MessageBox.Show(AppInstance.DeleMessage, AppInstance.SoftwareName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
            {
                return false;
            }
            foreach (DataGridViewRow row in this.LocalGridBrowser.SelectedRows)
            {
                DataRow row2 = ((DataRowView) row.DataBoundItem).Row;
                if (AppDataAccess.Delete(this.LocalTableName, row2[this.LocalKeyField]))
                {
                    this.LocalTable.Rows.Remove(row2);
                }
            }
            this.LocalTable.AcceptChanges();
            return true;
        }

        public virtual bool OnEdit()
        {
            if ((this.LocalTable == null) || (this.LocalTable.Rows.Count == 0))
            {
                return false;
            }
            if ((AppInstance.UserType == 1) && ((bool) this.LocalCurrentRow["GrpType"]))
            {
                MessageBox.Show("Không được sửa nhóm người sử dụng này!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            this.LocalFormEditor = new FormGroupEditor();
            if (this.LocalFormEditor != null)
            {
                this.LocalFormEditor.LocalFormLister = this;
                this.LocalFormEditor.NewChecked = false;
                this.LocalFormEditor.LocalCurrentRow = this.LocalCurrentRow;
                this.LocalFormEditor.Show();
            }
            return true;
        }

        public virtual void OnFillData()
        {
            if ((this.ColNames != null) && (this.ColNames.GetLength(0) != 0))
            {
                this.OnRefresh();
                if ((this.LocalTable != null) && !this.OrderFieldNames.Equals(string.Empty))
                {
                    ((BindingSource) this.LocalGridBrowser.DataSource).Sort = this.OrderFieldNames;
                }
                for (int i = 0; i < this.ColNames.GetLength(0); i++)
                {
                    if (((string) this.ColNames[i][2]) == "C")
                    {
                        DataGridViewTextBoxColumn dataGridViewColumn = new DataGridViewTextBoxColumn();
                        dataGridViewColumn.DataPropertyName = (string) this.ColNames[i][0];
                        dataGridViewColumn.HeaderText = (string) this.ColNames[i][1];
                        dataGridViewColumn.HeaderCell = new HeaderControl(dataGridViewColumn.HeaderCell);
                        this.LocalGridBrowser.Columns.Add(dataGridViewColumn);
                    }
                    else if (((string) this.ColNames[i][2]) == "B")
                    {
                        DataGridViewCheckBoxColumn column2 = new DataGridViewCheckBoxColumn();
                        column2.DataPropertyName = (string) this.ColNames[i][0];
                        column2.HeaderText = (string) this.ColNames[i][1];
                        column2.HeaderCell = new HeaderControl(column2.HeaderCell);
                        this.LocalGridBrowser.Columns.Add(column2);
                    }
                    else if (((string) this.ColNames[i][2]) == "N")
                    {
                        NumericColumn column3 = new NumericColumn();
                        column3.DataPropertyName = (string) this.ColNames[i][0];
                        column3.HeaderText = (string) this.ColNames[i][1];
                        column3.ZeroIsShown = false;
                        if (this.ColNames[i][3] != null)
                        {
                            column3.UserTextNumericFormat = (AppInstance.TextNumericFormat) this.ColNames[i][3];
                        }
                        else
                        {
                            column3.NumericPrecision = 10;
                            column3.NumericScaleOnFocus = 2;
                            column3.NumericScaleOnLostFocus = 2;
                        }
                        column3.HeaderCell = new HeaderControl(column3.HeaderCell);
                        this.LocalGridBrowser.Columns.Add(column3);
                    }
                }
            }
        }

        public virtual void OnRefresh()
        {
            this.LocalDataSet = new GroupDataAccess().GetDataSet();
            this.LocalGridBrowser.DataSource = new BindingSource(this.LocalDataSet, this.LocalTable.TableName);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.F2:
                    if (this.btnAdd.Enabled)
                    {
                        this.OnAdd();
                    }
                    return true;

                case Keys.F3:
                    if (this.btnEdit.Enabled)
                    {
                        this.OnEdit();
                    }
                    return true;

                case Keys.F4:
                    this.txtSearch.Focus();
                    return true;

                case Keys.F8:
                    if (this.btnDelete.Enabled)
                    {
                        this.OnDelete();
                    }
                    return true;

                case Keys.Escape:
                    base.Close();
                    return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        [Browsable(false)]
        public void SetPosition(string ps_FieldName, object ps_SearchedText)
        {
            if (this.LocalTable != null)
            {
                int num = 0;
                BindingSource dataSource = this.LocalGridBrowser.DataSource as BindingSource;
                num = dataSource.Find(ps_FieldName, ps_SearchedText);
                if (num < 0)
                {
                    DataView view = new DataView((DataTable) dataSource.DataSource, ps_FieldName + " LIKE '" + ps_SearchedText.ToString().Replace("'", "''") + "*'", "", DataViewRowState.OriginalRows);
                    if (view.Count > 0)
                    {
                        num = dataSource.Find(ps_FieldName, view[0][ps_FieldName]);
                    }
                }
                if (num >= 0)
                {
                    if (num == 0)
                    {
                        dataSource.Position = dataSource.Count - 1;
                        dataSource.Position = num;
                    }
                    else
                    {
                        dataSource.Position = num;
                    }
                }
            }
        }

        [Browsable(false), Category("Browser properties")]
        public object[][] ColNames
        {
            get
            {
                return this.is_ColNames;
            }
            set
            {
                this.is_ColNames = value;
            }
        }

        [Browsable(false), Category("Browser properties")]
        public string CurrentCode
        {
            get
            {
                return this.is_CurrentCode;
            }
            set
            {
                if (value == null)
                {
                    value = "";
                }
                this.is_CurrentCode = value;
                this.SetPosition(this.LocalFieldName, this.is_CurrentCode);
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public DataRow LocalCurrentRow
        {
            get
            {
                try
                {
                    if ((this.LocalGridBrowser.CurrentRow == null) || (this.LocalGridBrowser.CurrentRow.DataBoundItem == null))
                    {
                        this.oCurrentRow = null;
                    }
                    else
                    {
                        this.oCurrentRow = ((DataRowView) this.LocalGridBrowser.CurrentRow.DataBoundItem).Row;
                    }
                }
                catch
                {
                    this.oCurrentRow = null;
                }
                return this.oCurrentRow;
            }
            set
            {
                this.oCurrentRow = value;
            }
        }

        [Browsable(false), Category("Browser properties")]
        public DataSet LocalDataSet
        {
            get
            {
                return this.oLocalDataSet;
            }
            set
            {
                this.oLocalDataSet = value;
            }
        }

        [Category("Browser properties")]
        public string LocalFieldName
        {
            get
            {
                return this.is_FieldName;
            }
            set
            {
                this.is_FieldName = value;
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public FormGroupEditor LocalFormEditor
        {
            get
            {
                return this.oFormEditor;
            }
            set
            {
                this.oFormEditor = value;
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public GridControl LocalGridBrowser
        {
            get
            {
                return this.grdBrowser;
            }
        }

        [Category("Browser properties")]
        public string LocalKeyField
        {
            get
            {
                return this.is_LocalKeyField;
            }
            set
            {
                this.is_LocalKeyField = value;
            }
        }

        [Category("Browser properties"), Browsable(false)]
        public DataTable LocalTable
        {
            get
            {
                return this.LocalDataSet.Tables[0];
            }
        }

        [Category("Browser properties")]
        public string LocalTableName
        {
            get
            {
                return this.is_LocalTableName;
            }
            set
            {
                if (this.is_LocalTableName != value)
                {
                    this.is_LocalTableName = value;
                }
            }
        }

        [Category("Browser properties")]
        public string OrderFieldNames
        {
            get
            {
                return this.is_OrderFieldNames;
            }
            set
            {
                if (value.Equals(string.Empty))
                {
                    value = "";
                }
                this.is_OrderFieldNames = value;
            }
        }
    }
}

